#!/usr/bin/env python

import wsgiref.handlers

from google.appengine.ext import webapp

from django.utils import simplejson

from entities import document

class PostHandler(webapp.RequestHandler):
    def get(self):
        document_id = self.request.get("id")
        document_content = self.request.get("documentContent")
        
        if(document_id != '' and document_content != ''):
            doc = document.Document.get_by_id(int(document_id))
            if ( doc ):
                doc.documentContent = document_content
                doc.isUpdated = True
                doc.put()
                
                self.documentJSONResponse(doc)
            else:
                self.errorResponse("Can't found document with id"+str(document_id))
                
        else:
            self.errorResponse("You must provide id and content for this request")
            
    def errorResponse(self, message):
        errorData = {"error":message}
        self.response.out.write(simplejson.dumps(errorData))
        
    def documentJSONResponse(self, doc):
        docData = {
            "id":doc.key().id()
            }
        self.response.out.write(simplejson.dumps(docData))
        
